/*********************************************************
 *
 *  Project:      GrabChineseLessons
 *
 *  Class:        HZAddAccessor
 *
 *  File name:    HZAddAccessor.java
 *
 *  Parent class:
 *
 *********************************************************/

package com.eastmountain.chinese.db.flashcards.access;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.logging.Level;

import com.eastmountain.chinese.db.exceptions.DBAccessException;
import com.eastmountain.chinese.db.flashcards.connection.DBAccessData;

public class HZFinishDBCreationAccessor extends HZAccessor {

    /***************************************************************************
     * 
     * CLASS - HZFinishDBCreationAccessor
     * 
     **************************************************************************/

    public HZFinishDBCreationAccessor( DBAccessData dbURLs )
            throws DBAccessException {

        super( dbURLs );    
        
        logger.setLevel( Level.SEVERE );
        
        try {
            
            flashCardDBConnection.setAutoCommit( true );
            
        } catch ( SQLException sqlException ) {

            logger.log( Level.SEVERE, "\nSQLException: "
                    + sqlException.getMessage() + "\n" + "SQLState: "
                    + sqlException.getSQLState() + "\n" + "VendorError: "
                    + sqlException.getErrorCode() );

        } catch ( Exception exception ) {

            logger.log( Level.SEVERE, "\nUnexpected Exception: "
                    + exception.getMessage() + "\n" );

        }
   
    }

    /***************************************************************************
     * 
     * compressDB
     * 
     **************************************************************************/

    public boolean compressDB() {

        CallableStatement callableStatement;
        boolean result = false;

        try {
/*
            callableStatement = flashCardDBConnection
                    .prepareCall( "CALL SYSCS_UTIL.SYSCS_COMPRESS_TABLE(?, ?, ?)" );

            callableStatement.setString( 1, "APP" );
            callableStatement.setString( 2, "WORDS" );
            callableStatement.setShort( 3, (short) 1 );
            
*/
            
            callableStatement = flashCardDBConnection.prepareCall
            ("CALL SYSCS_UTIL.SYSCS_INPLACE_COMPRESS_TABLE(?, ?, ?, ?, ?)");
            callableStatement.setString(1, "APP");
            callableStatement.setString(2, "WORDS");
            callableStatement.setShort(3, (short) 1);
            callableStatement.setShort(4, (short) 1);
            callableStatement.setShort(5, (short) 1);
            
            callableStatement.execute();
            callableStatement.close();

            result = true;
            
            System.out.println( "****   DB compression completed.   ****");

        } catch ( SQLException sqlException ) {

            logger.log( Level.SEVERE, "\nSQLException: "
                    + sqlException.getMessage() + "\n" + "SQLState: "
                    + sqlException.getSQLState() + "\n" + "VendorError: "
                    + sqlException.getErrorCode() );

        } catch ( Exception exception ) {

            logger.log( Level.SEVERE, "\nUnexpected Exception: "
                    + exception.getMessage() + "\n" );

        }

        return result;

    }

    /***************************************************************************
     * 
     * setDBProperties
     * 
     **************************************************************************/

    public boolean setDBProperties() {

        CallableStatement callableStatement;
        boolean result = false;

        try {

            callableStatement = flashCardDBConnection
                    .prepareCall( "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)" );

            callableStatement.setString( 1, "derby.storage.tempDirectory" );
            callableStatement.setString( 2, "db/temp" );
            callableStatement.execute();

            callableStatement.setString( 1, "derby.stream.error.file" );
            callableStatement.setString( 2, "db/temp/derby.log" );
            callableStatement.execute();

            callableStatement.setString( 1, "derby.system.home" );
            callableStatement.setString( 2, "/db" );
            callableStatement.execute();

            callableStatement.close();

            result = true;
            
            System.out.println( "****   DB Properties set.   ****");

        } catch ( SQLException sqlException ) {

            logger.log( Level.SEVERE, "\nSQLException: "
                    + sqlException.getMessage() + "\n" + "SQLState: "
                    + sqlException.getSQLState() + "\n" + "VendorError: "
                    + sqlException.getErrorCode() );

        } catch ( Exception exception ) {

            logger.log( Level.SEVERE, "\nUnexpected Exception: "
                    + exception.getMessage() + "\n" );

        }

        return result;

    }

    @Override
    protected void setupPreparedStatements() throws DBAccessException {

    }

}